// pages/sharebox/shareDevDetail/shareDevDetail.js
var app = getApp();
var _PAGE = 1;
var _PAGESIZE = 10;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowModal: false,
    hasMore: false,
    load: true,
    loading: false,
    shareList: [],
    id:0,
    bind_id:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    that.getshareList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (!that.data.hasMore) {
      return false;
    }
    _PAGE++;
    that.setData({
      loading: true
    })
    that.getshareList();
  },

  //删除共享
  deleteShare: function(e) {
    var that = this;
    var bind_id = e.currentTarget.dataset.bind_id;
    that.setData({
      isShowModal: true,
      bind_id: bind_id
    })
  },

  //确认
  deleteSure: function () {
    var that = this;
    app.ajax({
      url: "User/Shared/unBindDevice",
      data: {
        bind_id: that.data.bind_id
      },
      success: function (res) {
        that.getshareList();
        app.showToast(res.data.message);
      }
    })
    that.setData({
      isShowModal: false,
    })
  },

  //取消
  cancel: function() {
    var that = this;
    that.setData({
      isShowModal: false,
    })
  },

  // 添加共享
  addshare() {
    var that = this;
    wx.navigateTo({
      url: '../pattern/pattern?id=' + that.data.id,
    })   
  },

  // 获取共享列表
  getshareList() {
    var that = this;
    app.ajax({
      url: "User/Shared/sharedDevicePeople",
      data: {
        page: _PAGE,
        row: _PAGESIZE,
        device_id:that.data.id
      },
      success: function(res) {
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            that.setData({
              shareList: res.data.data.data
            })
          } else {
            that.setData({
              shareList: that.data.shareList.concat(res.data.data.data)
            })
          }
          // 是否加载更多
          var hasMore = true;
          if (res.data.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            load: false,
            loading: false
          });
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false
            })
          }
        }
      }
    })
  }

})